/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package emprestimo.frames;

import livro.control.*;
import bibliinfo.FrameMain;
import java.util.ArrayList;
import java.util.List;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import livro.model.Livro;
import livro.model.LivroDao;
import livro.model.LivroExe;
import livro.model.ValidaLivro;

/**
 *
 * @author Ar0n
 */
public class LoanFrameSave extends javax.swing.JInternalFrame {

    /**
     * Creates new form UserFrameDadosSalve
     */
    public LoanFrameSave() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365287991_edit-clear.png"))); // NOI18N
        jButton2.setText("Limpar Campos");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365683699_window-close.png"))); // NOI18N
        jButton3.setText("Cancelar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/img_user/1365288060_3floppy_umount.png"))); // NOI18N
        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 165, Short.MAX_VALUE)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(60, 60, 60))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(62, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        
       FrameMain.loanFrameCad.setVisible(false);
       FrameMain.loanFrameCad.setEstado(false);
       
       FrameMain.loanFrameMain.setVisible(true);
       FrameMain.loanFrameMain.setEstado(true); 
        
        //retirando bordas do internalJframe           
       ((BasicInternalFrameUI)FrameMain.loanFrameMain.getUI()).setNorthPane(null);
       FrameMain.loanFrameMain.setBorder(null);        
       //pegando dimenções do JFrame no qual ele esta encapsulado
       FrameMain.loanFrameMain.setBounds(0, 0, FrameMain.jDesktop.getWidth(), FrameMain.jDesktop.getHeight());        
       FrameMain. loanFrameMain.setResizable(false);
       FrameMain.loanFrameMain.setBorder(null);       
        
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
      limparCampos();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ValidaLivro valida = new ValidaLivro();
        boolean resp = valida.validaCad(FrameMain.bookFrameCad.jTTitulo, FrameMain.bookFrameCad.jTAutor, FrameMain.bookFrameCad.jTEditora,
                FrameMain.bookFrameCad.jTISBN, FrameMain.bookFrameCad.jTQuant);        
        if(resp == true){
            SalvarLivros();            
        }        
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    // End of variables declaration//GEN-END:variables
    //métodos
    public void limparCampos(){
        FrameMain.loanFrameCad.jTRg.setText("");
        FrameMain.loanFrameCad.jTISBN.setText("");
        FrameMain.loanFrameCad.jTNumSeq.setText("");
        
    }
    
    public void SalvarLivros(){
        Livro livro = new Livro();
        livro.setTitulo(FrameMain.bookFrameCad.jTTitulo.getText());
        livro.setAutor(FrameMain.bookFrameCad.jTAutor.getText());
        livro.setEditora(FrameMain.bookFrameCad.jTEditora.getText());
        livro.setISBN(FrameMain.bookFrameCad.jTISBN.getText());
        livro.setQuant(Integer.parseInt(FrameMain.bookFrameCad.jTQuant.getText()));
        livro.setAtivo(true);
        
        List<LivroExe> lista = new ArrayList<>();
        for(int i = 0; i < livro.getQuant(); i++){
            LivroExe livroEx = new LivroExe();
            livroEx.setISBN(livro.getISBN());
            livroEx.setNumSequencia(i);
            livroEx.setEmprestado(false);
            livroEx.setPodeEmprestar(true);
            
            lista.add(livroEx);
        }
        
        LivroDao dao = new LivroDao();
        dao.create(livro, lista, null, null);
        
        FrameMain.bookFrameCad.setVisible(false);
        FrameMain.bookFrameCad.setEstado(false);
        
        FrameMain.bookFrameMain.setVisible(true);
        FrameMain.bookFrameMain.setEstado(true);
        FrameMain.formata(21);
    }
}
